luci-app-banip: feed editor improvements
authorDirk Brenken <[email protected]>
Fri, 28 Apr 2023 10:41:36 +0000 (12:41 +0200)
committerDirk Brenken <[email protected]>
Fri, 28 Apr 2023 10:41:36 +0000 (12:41 +0200)
* add the ability to up-/download a local/remote JSON feed file

Signed-off-by: Dirk Brenken <[email protected]>
(cherry picked from commit ee3f56bd02aec46fd73f81e7470f213579534a9c)
Signed-off-by: Dirk Brenken <[email protected]>
applications/luci-app-banip/htdocs/luci-static/resources/view/banip/custom.css
applications/luci-app-banip/htdocs/luci-static/resources/view/banip/feeds.js
applications/luci-app-banip/root/usr/share/rpcd/acl.d/luci-app-banip.json

index 4125e8892459f7f0924a2cafa28ebdaad243a942..23c60c683d20866bcae6dc51fd33ee7a1c7bb01a 100644 (file)
@@ -1,3 +1,3 @@
 .cbi-input-text {
        width: 90% !important;
-}
\ No newline at end of file
+}
index 672b23d0f85c17c4bca896fa465e1db6501e308e..30fcb9d0470d8d0b81671f9b1fb25ee7012acc26 100644 (file)
@@ -30,12 +30,14 @@ const observer = new MutationObserver(function (mutations) {
                        label.setAttribute("style", "font-weight: bold !important; color: #595 !important;");
                })
                L.resolveDefault(fs.stat('/etc/banip/banip.custom.feeds'), '').then(function (stat) {
-                       const buttons = document.querySelectorAll('#btnClear, #btnCreate, #btnSave');
-                       if (buttons[0] && stat.size === 0) {
-                               buttons[0].removeAttribute('disabled');
-                       } else if (buttons[1] && buttons[2] && stat.size > 0) {
+                       const buttons = document.querySelectorAll('#btnClear, #btnCreate, #btnSave, #btnUpload, #btnDownload');
+                       if (buttons[1] && buttons[2] && stat.size === 0) {
                                buttons[1].removeAttribute('disabled');
                                buttons[2].removeAttribute('disabled');
+                       } else if (buttons[0] && buttons[3] && buttons[4] && stat.size > 0) {
+                               buttons[0].removeAttribute('disabled');
+                               buttons[3].removeAttribute('disabled');
+                               buttons[4].removeAttribute('disabled');
                        }
                });
        }
@@ -54,6 +56,31 @@ observer.observe(targetNode, observerConfig);
        button handling
 */
 function handleEdit(ev) {
+       if (ev === 'upload') {
+               return ui.uploadFile('/etc/banip/banip.custom.feeds').then(function () {
+                       L.resolveDefault(fs.read_direct('/etc/banip/banip.custom.feeds', 'json'), "").then(function (res) {
+                               if (res) {
+                                       location.reload();
+                               } else {
+                                       fs.write('/etc/banip/banip.custom.feeds', null).then(function () {
+                                               ui.addNotification(null, E('p', _('Upload of the custom feed file failed.')), 'error');
+                                       });
+                               }
+                       });
+               }).catch(function () { });
+       }
+       if (ev === 'download') {
+               return fs.read_direct('/etc/banip/banip.custom.feeds', 'blob').then(function (blob) {
+                       let url = window.URL.createObjectURL(blob),
+                               date = new Date(),
+                               name = 'banip.custom.feeds_%04d-%02d-%02d.json'.format(date.getFullYear(), date.getMonth() + 1, date.getDate()),
+                               link = E('a', { 'style': 'display:none', 'href': url, 'download': name });
+                       document.body.appendChild(link);
+                       link.click();
+                       document.body.removeChild(link);
+                       window.URL.revokeObjectURL(url);
+               }).catch(function () { });
+       }
        if (ev === 'create') {
                return fs.read_direct('/etc/banip/banip.feeds', 'json').then(function (content) {
                        fs.write('/etc/banip/banip.custom.feeds', JSON.stringify(content)).then(function () {
@@ -104,7 +131,7 @@ function handleEdit(ev) {
                }
                sumSubElements.push(nodeKeys[i].value, subElements);
        }
-       exportJson = JSON.stringify(sumSubElements).replace(/,{/g, ':{').replace(/^\[/g, '{').replace(/\]$/g, '}');
+       exportJson = JSON.stringify(sumSubElements).replace(/,{/g, ':{').replace(/^\[/, '{').replace(/\]$/, '}');
        return fs.write('/etc/banip/banip.custom.feeds', exportJson).then(function () {
                location.reload();
        });
@@ -118,9 +145,9 @@ return view.extend({
        render: function (data) {
                let m, s, o, feed, url_4, url_6, rule_4, rule_6, descr, flag;
 
-               m = new form.JSONMap(data, 'Custom Feed Editor', _('With this editor you can fill up an initial custom feed file (a 1:1 copy of the version shipped with the package). \
+               m = new form.JSONMap(data, 'Custom Feed Editor', _('With this editor you can upload your local custom feed file or fill up an initial one (a 1:1 copy of the version shipped with the package). \
                        The file is located at \'/etc/banip/banip.custom.feeds\'. \
-                       Then you can edit this file, delete entries, add new ones, etc. To go back to the maintainers version just empty the custom feed file again (do not delete it!).'));
+                       Then you can edit this file, delete entries, add new ones or make a local backup. To go back to the maintainers version just empty the custom feed file again (do not delete it!).'));
                for (let i = 0; i < Object.keys(m.data.data).length; i++) {
                        feed = Object.keys(m.data.data)[i];
                        url_4 = m.data.data[feed].url_4;
@@ -198,6 +225,24 @@ return view.extend({
                s = m.section(form.NamedSection, 'global');
                s.render = L.bind(function () {
                        return E('div', { class: 'right' }, [
+                               E('button', {
+                                       'class': 'btn cbi-button cbi-button-action',
+                                       'id': 'btnDownload',
+                                       'disabled': 'disabled',
+                                       'click': ui.createHandlerFn(this, function () {
+                                               return handleEdit('download');
+                                       })
+                               }, [_('Download Custom Feeds')]),
+                               '\xa0\xa0\xa0',
+                               E('button', {
+                                       'class': 'btn cbi-button cbi-button-action',
+                                       'id': 'btnUpload',
+                                       'disabled': 'disabled',
+                                       'click': ui.createHandlerFn(this, function () {
+                                               return handleEdit('upload');
+                                       })
+                               }, [_('Upload Custom Feeds')]),
+                               '\xa0\xa0\xa0\xa0\xa0\xa0',
                                E('button', {
                                        'class': 'btn cbi-button cbi-button-action important',
                                        'id': 'btnCreate',
index 395e06aadc72099149150688c65007ab776c3a73..41b4b6650b630afd830658d8a385e67236043acf 100644 (file)
@@ -4,7 +4,8 @@
                "write": {
                        "file": {
                                "/etc/banip/*": [
-                                       "read"
+                                       "read",
+                                       "write"
                                ],
                                "/etc/banip/banip.allowlist": [
                                        "write"
@@ -13,6 +14,7 @@
                                        "write"
                                ],
                                "/etc/banip/banip.custom.feeds": [
+                                       "read",
                                        "write"
                                ]
                        },